package jsptag;

import db.exceptions.PatientDaoException;
import db.exceptions.PersonDaoException;
import personal.Doctor;
import personal.Patient;
import personal.Person;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.Collection;

/**
 * User: Vanil
 * Date: 17.12.2010
 * Time: 12:52:33
 */
public class DoctorsHasRightsTag extends TagSupport {
    private Person user;

    public void setUser(Person user) {
        this.user = user;
    }

    @Override
    public int doStartTag() throws JspException {

        Person sessionUser = (Person) pageContext.getSession().getAttribute("sessionUser");
        Person displayedUser = (Person) pageContext.getAttribute("displayedUser");
        try {
        Collection<Patient> patientCollection = ((Doctor) sessionUser).getPatients();
        if(sessionUser.equals(user))
            return EVAL_BODY_INCLUDE;
        for(Patient pat:patientCollection)
        {
            if (pat.equals(user))
            {
                return EVAL_BODY_INCLUDE;
            }
        }

        }
        catch (PersonDaoException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (PatientDaoException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return SKIP_BODY;    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public int doEndTag() throws JspException {
        return EVAL_PAGE;    //To change body of overridden methods use File | Settings | File Templates.
    }
}
